let categoryApp=new Vue({
    el:"#categoryApp",
    data:{
        categories:[],
        getCatId:""
    },
    methods:{
        loadCategory:function () {
            axios({
                url:"/categories",
                method:"get"
            }).then(function (response) {
                categoryApp.categories=response.data
            })
        },
        addCategory:function () {
            let form=new FormData;
            let catName=$("#categoryName").val()
            if (catName == null){
                alert("分类名称不能为空")
                return;
            }
            form.append("catName",catName);
            axios({
                url:"/categories/add/",
                method:"post",
                data:form
            }).then(function (response) {
                if (typeof response.data==="object"){
                    categoryApp.categories.push(response.data)
                }else {
                    alert(response.data)
                }

            })
        },
        updateCategory:function () {
            let newCatName=$("#UpdateCategoryName").val()
            if (newCatName == null){
                alert("分类名称不能为空")
                return;
            }
            let catId=categoryApp.getCatId
            axios({
                url:"/categories/update/"+catId+"/"+newCatName,
                method:"get"
            }).then(function (response) {
                if (typeof response.data ==="object"){
                    categoryApp.loadCategory()
                }else {
                    alert(response.data)
                }

            })
        },
        deleteCategory:function (catId,index) {
            axios({
                url:"/categories/delete/"+catId,
                method:"get"
            }).then(function (response) {
                if (response.data == "ok"){
                    categoryApp.categories.splice(index,1)
                }else {
                    alert(response.data)
                }
            })
        },
        getInfo:function (catId) {
            categoryApp.getCatId=catId
            $("#myModalLabel2").text("修改");
            $("#myModalUpdate").modal();
        }
    },

    created:function () {
        this.loadCategory()
    }
})